package com.wang.experiment.experiment3.horseracing;

import sun.security.jca.GetInstance;

import java.util.concurrent.*;

/**
 * @author wr
 * @date 2021/10/27 20:25
 */
public class RaceService {
    private final Integer maxNum;
    private ExecutorService service;

    public RaceService(Integer maxNum) {
        this.maxNum = maxNum;
    }

    public ExecutorService getServiceInstance() {
        if (service == null || service.isTerminated()) {
            //阿里巴巴编码规范要求手动创建线程池qwq
            service = new ThreadPoolExecutor(
                    maxNum, maxNum,
                    10,
                    TimeUnit.SECONDS,
                    new PriorityBlockingQueue<>(),
                    new ThreadFactory() {
                        @Override
                        public Thread newThread(Runnable r) {
                            //为线程命名
                            return new Thread(r, r.toString());
                        }
                    });
        }
        return service;
    }
}
